package com.xyd.Servlet;

import com.xyd.Biz.Impl.NewsBizImpl;
import com.xyd.Biz.NewsBiz;
import com.xyd.Entity.ManageUser;
import com.xyd.Entity.News;
import com.xyd.Util.JsonServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/html/manage/manageNewsServlet")
public class manageNewsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op =  req.getParameter("op");
        if(op.equals("insert")){
            insertNews(req, resp);
        }else if(op.equals("update")){
            updateNews(req, resp);
        }else if(op.equals("delete")){
            deleteNews(req, resp);
        }
    }

    public void deleteNews(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        NewsBiz newsBiz = new NewsBizImpl();
        String id = req.getParameter("id");

        //ManageUser manageUser = (ManageUser) req.getSession().getAttribute("manageUser");
        //news.setNewsuserid(manageUser.getUserid());

        int flag =newsBiz.deleteNews(Integer.parseInt(id));
        if(flag>0){
            resp.setContentType("text/html;charset=GBK");
            resp.getWriter().write("<script>" +
                    "alert('删除成功');"+
                    "location.href='news.jsp';"
                    +"</script>");

        }else{
            resp.setContentType("text/html;charset=GBK");
            resp.getWriter().write("<script>" +
                    "alert('删除出错');"+
                    "location.href='news.jsp';"
                    +"</script>");
            req.getRequestDispatcher("news.jsp").forward(req,resp);
        }
    }

    public void insertNews(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        NewsBiz newsBiz = new NewsBizImpl();
        String title = req.getParameter("newstittle");
        String type = req.getParameter("type");
        String context = req.getParameter("context");
        News news = new News();
        news.setNewstitle(title);
        news.setNewscontent(context);
        news.setNewstype(Integer.parseInt(type));
        //ManageUser manageUser = (ManageUser) req.getSession().getAttribute("manageUser");
        //news.setNewsuserid(manageUser.getUserid());

        int flag =newsBiz.insertNews(news);
        if(flag>0){
            resp.setContentType("text/html;charset=GBK");
            resp.getWriter().write("<script>" +
                    "alert('添加成功');"+
                    "location.href='news.jsp';"
                    +"</script>");

        }else{
            resp.setContentType("text/html;charset=GBK");
            resp.getWriter().write("<script>" +
                    "alert('添加出错');"+
                    "location.href='news.jsp';"
                    +"</script>");
            req.getRequestDispatcher("news.jsp").forward(req,resp);
        }
    }

    public void updateNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsBiz newsBiz = new NewsBizImpl();
        String id = req.getParameter("id");
        String newstittle = req.getParameter("newstittle");
        String type = req.getParameter("type");
        String context = req.getParameter("context");
        News news = new News();
        news.setNewsid(Integer.parseInt(id));
        news.setNewstype(Integer.parseInt(type));
        news.setNewscontent(context);
        news.setNewstitle(newstittle);

        int i = newsBiz.updateNews(news);
        if(i>0){
            resp.setContentType("text/html;charset=GBK");
            resp.getWriter().write("<script>" +
                    "alert('修改成功');"+
                    "location.href='news.jsp';"
                    +"</script>");

        }else{
            resp.setContentType("text/html;charset=GBK");
            resp.getWriter().write("<script>" +
                    "alert('修改出错');"+
                    "location.href='news.jsp';"
                    +"</script>");
            req.getRequestDispatcher("news.jsp").forward(req,resp);
        }

    }
}
